package com.educhains.module.post.service.impl;

import com.educhains.module.demo.vo.Page;
import com.educhains.module.post.persist.mybatis.PostContentMapper;
import com.educhains.module.post.persist.mybatis.PostMapper;
import com.educhains.module.post.persist.mybatis.PostReplyMapper;
import com.educhains.module.post.service.PostService;
import com.educhains.module.post.vo.Post;
import com.educhains.module.post.vo.PostContent;
import com.educhains.module.post.vo.PostReply;
import com.educhains.module.post.vo.PostWithContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class PostServiceImpl implements PostService{

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private PostContentMapper postContentMapper;


    public Post selectPostByPrimaryKey(String postId) {
        return postMapper.selectByPrimaryKey(postId);
    }

    public PostContent selectPostContentByPrimaryKey(String postId) {
        return postContentMapper.selectByPrimaryKey(postId);
    }

    public int insertSelective(Post record) {
        return postMapper.insertSelective(record);
    }

    public int updatePostByPrimaryKeySelective(Post record) {
        return postMapper.updateByPrimaryKeySelective(record);
    }

    public int updatePostContentByPrimaryKeySelective(PostContent record) {
        return postContentMapper.updateByPrimaryKeySelective(record);
    }

    public int insertContent(PostContent record) {
        return postContentMapper.insert(record);
    }

    public void addReadNum(String postId){ postMapper.addReadNum(postId);}


    public List<Post> selectPostList() {
        return postMapper.selectPostList();
    }

    public List<PostWithContent> showByPage(int start, int end) {
        return postMapper.showByPage(start, end);
    }

    public Page showPostPage(int pageNum, int option) {
        int totalRecord = selectPostList().size();
        List<PostWithContent> list;
        Page page = new Page(pageNum, totalRecord);
        page.setPageSize(10);
        switch (option){
            case 1:
                list = postMapper.showByPage(page.getStartIndex(), page.getPageSize());
                break;
            case 2:
                list = postMapper.showByPageSortByView(page.getStartIndex(), page.getPageSize());
                break;
            case 3:
                list = postMapper.showByPageSortByComment(page.getStartIndex(), page.getPageSize());
                break;
            default:
                list = postMapper.showByPage(page.getStartIndex(), page.getPageSize());
        }
        page.setList(list);
        return page;
    }

    public PostWithContent selectPostDetail(String postId) {
        return new PostWithContent(postMapper.selectByPrimaryKey(postId), postContentMapper.selectByPrimaryKey(postId));
    }

    public Page searchByTitlePage(int pageNum, String titleFragment) {
        titleFragment = "%" + titleFragment+ "%";
        int totalRecord = postMapper.searchByTitle(titleFragment).size();
        Page page = new Page(pageNum, totalRecord);
        List list = postMapper.searchByTitlePage(page.getStartIndex(), page.getPageSize(), titleFragment);
        page.setList(list);
        return page;
    }

    public List<Post> selectPostListLimit(int postNum, String type) {
        return postMapper.selectPostListLimit(postNum, type);
    }

    public int deletePostByPrimaryKey(String postId) {
        return postMapper.deleteByPrimaryKey(postId);
    }

    public List<Post> getPostListByUserIdWithLimit(String userId, int limit) {
        return postMapper.getPostListByUserIdWithLimit(userId, limit);
    }
}
